# If running scripts one after the other, then should remove some elements because names are reused
## rm(list=ls())
## Run after 1993_Figures_Replication, 2002_Figures_Replication, and 2008_Figures_Replication scripts
Sys.setlocale("LC_CTYPE", "EN_US.UTF-8")
setwd("~/Dropbox/Dissertation/Ideological_Labels/Replication")
library(foreign)
library(broom)
options(scipen = 999)

library(ggplot2)
library(scales)

# load our data
load("symCorrs.Rda")
load("symCorrs02.Rda")
load("symCorrs08.Rda")
load("symCorrs90.Rda")
load("symCorrs0290.Rda")

# Combine everything into one data frame

orderedCorrs.all <- rbind(orderedCorrs.sym, orderedCorrs.sym02, orderedCorrs.sym08)

# Remove a few rows

orderedCorrs.all <- orderedCorrs.all[-c(11:16),]

## Add in 90 percent confidence intervals

orderedCorrs.all90 <- rbind(orderedCorrs.sym90, orderedCorrs.sym0290)

# Create list of partisan vs. symbolic issues

issueClass <- c("Partisan", "Symbolic", "Partisan", "Symbolic", "Symbolic", "Partisan", "Partisan", "Symbolic", "Symbolic", "Partisan")

orderedCorrs.all <- cbind(orderedCorrs.all, issueClass)
orderedCorrs.all90 <- cbind(orderedCorrs.all90, issueClass)

credplot.gg <- function(d1, d2){
  # d is a data frame with 4 columns
  # d$x gives variable names
  # d$y gives center point
  # d$ylo gives lower limits
  # d$yhi gives upper limits
  require(ggplot2)
  p <- ggplot(d1, aes(y=symLabels, x=estimate, xmin=conf.low, xmax=conf.high)) +
    geom_point(aes(shape = issueClass), size = 2.5) +
    geom_errorbarh(height = 0, size = 1) +
    geom_errorbarh(height = 0, data = d2, aes(xmin=conf.low, xmax=conf.high)) +
    geom_vline(xintercept = 0, linetype=5) +
    geom_vline(xintercept = -0.13, linetype=3) +
    geom_vline(xintercept = 0.13, linetype=3) +
    scale_x_continuous(breaks = c(-0.5, -0.4, -0.3, -0.2, -0.1, 0, 0.1, 0.2, 0.3, 0.4, 0.5), limits = c(-0.59, 0.59)) +
    xlab('Correlation with Left-Right Self-Identification') + 
    ylab('') +
    facet_grid(issueType ~ ., scales = "free", space = "free") +
    theme_bw() + 
    theme(strip.text.y = element_text(angle = 0), text = element_text(size=12), legend.position = 'none') + 
    scale_shape_manual(values = c(16, 21))
  return(p)
}

## Figure 8
pdf("Combined_L-R_correlations_sym90.pdf", width = 8.5, height = 6)
credplot.gg(orderedCorrs.all90, orderedCorrs.all)
dev.off()